package com.byxy.dao;

import com.byxy.model.FlowInfo;
import com.byxy.util.DBUtil;
import com.byxy.util.DateUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FlowInfoDaoImpl implements BaseDao<FlowInfo> {
	private PreparedStatement psta = null;
	private Connection conn = null;
	private ResultSet rs = null;

	@Override
	public int add(FlowInfo flow) {
		int i = -1;
		int id = flow.getId();
		String name = flow.getName();
		String sex = flow.getSex();
		String familyName = flow.getFamilyName();
		Date birthday = flow.getBirthday();
		int partyStanding = flow.getPartyStanding();
		String education = flow.getEducation();
		String posted = flow.getPosted();
		Date deliveryHours = flow.getDeliveryHours();
		String exitType = flow.getExitType();
		String outgoingFlow = flow.getOutgoingFlow();
		String partyOrganizationCategory = flow.getPartyOrganizationCategory();
		String contactWithPartyBranch = flow.getContactWithPartyBranch();
		boolean mobilePartyCard = flow.isMobilePartyCard();
		String outflowBranchContactName = flow.getOutflowBranchContactName();
		String outflowBranchContactPhone = flow.getOutflowBranchContactPhone();
		String inflowBranchContactName = flow.getInflowBranchContactName();
		String inflowBranchContactPhone = flow.getInflowBranchContactPhone();
		String contactNumber = flow.getContactNumber();
		int qqNumber = flow.getQqNumber();
		String wechatNumber = flow.getWechatNumber();
		int departmentId = flow.getDepartmentId();
		String sql = "INSERT INTO FlowInfo VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			psta.setString(2, name);
			psta.setString(3, sex);
			psta.setString(4, familyName);
			psta.setTimestamp(5, DateUtil.dateToTimestamp(birthday));
			psta.setInt(6, partyStanding);
			psta.setString(7, education);
			psta.setString(8, posted);
			psta.setTimestamp(9, DateUtil.dateToTimestamp(deliveryHours));
			psta.setString(10, exitType);
			psta.setString(11, outgoingFlow);
			psta.setString(12, partyOrganizationCategory);
			psta.setString(13, contactWithPartyBranch);
			int mobilePartyCardResult = 0;
			if (mobilePartyCard) {
				mobilePartyCardResult = 1;
			}
			psta.setInt(14, mobilePartyCardResult);
			psta.setString(15, outflowBranchContactName);
			psta.setString(16, outflowBranchContactPhone);
			psta.setString(17, inflowBranchContactName);
			psta.setString(18, inflowBranchContactPhone);
			psta.setString(19, contactNumber);
			psta.setInt(20, qqNumber);
			psta.setString(21, wechatNumber);
			psta.setInt(22, departmentId);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int deleteById(int id) {
		int i = -1;
		String sql = "DELETE FROM FlowInfo WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int update(FlowInfo flow) {
		int i = -1;
		int id = flow.getId();
		String name = flow.getName();
		String sex = flow.getSex();
		String familyName = flow.getFamilyName();
		Date birthday = flow.getBirthday();
		int partyStanding = flow.getPartyStanding();
		String education = flow.getEducation();
		String posted = flow.getPosted();
		Date deliveryHours = flow.getDeliveryHours();
		String exitType = flow.getExitType();
		String outgoingFlow = flow.getOutgoingFlow();
		String partyOrganizationCategory = flow.getPartyOrganizationCategory();
		String contactWithPartyBranch = flow.getContactWithPartyBranch();
		boolean mobilePartyCard = flow.isMobilePartyCard();
		String outflowBranchContactName = flow.getOutflowBranchContactName();
		String outflowBranchContactPhone = flow.getOutflowBranchContactPhone();
		String inflowBranchContactName = flow.getInflowBranchContactName();
		String inflowBranchContactPhone = flow.getInflowBranchContactPhone();
		String contactNumber = flow.getContactNumber();
		int qqNumber = flow.getQqNumber();
		String wechatNumber = flow.getWechatNumber();
		int departmentId = flow.getDepartmentId();
		String sql = "UPDATE FlowInfo SET name = ?, sex = ?, familyName = ?, birthday = ?, partyStanding = ?, education = ?, posted = ?, deliveryHours = ?, exitType = ?, outgoingFlow = ?, partyOrganizationCategory = ?, contactWithPartyBranch = ?, mobilePartyCard = ?, outflowBranchContactName = ?, outflowBranchContactPhone = ?, inflowBranchContactName = ?, inflowBranchContactPhone = ?, contactNumber = ?, qqNumber = ?, wechatNumber = ? ,departmentId = ? WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, name);
			psta.setString(2, sex);
			psta.setString(3, familyName);
			psta.setTimestamp(4, DateUtil.dateToTimestamp(birthday));
			psta.setInt(5, partyStanding);
			psta.setString(6, education);
			psta.setString(7, posted);
			psta.setTimestamp(8, DateUtil.dateToTimestamp(deliveryHours));
			psta.setString(9, exitType);
			psta.setString(10, outgoingFlow);
			psta.setString(11, partyOrganizationCategory);
			psta.setString(12, contactWithPartyBranch);
			int mobilePartyCardResult = 0;
			if (mobilePartyCard) {
				mobilePartyCardResult = 1;
			}
			psta.setInt(13, mobilePartyCardResult);
			psta.setString(14, outflowBranchContactName);
			psta.setString(15, outflowBranchContactPhone);
			psta.setString(16, inflowBranchContactName);
			psta.setString(17, inflowBranchContactPhone);
			psta.setString(18, contactNumber);
			psta.setInt(19, qqNumber);
			psta.setString(20, wechatNumber);
			psta.setInt(21, departmentId);
			psta.setInt(22, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public List<FlowInfo> findAll() {
		List<FlowInfo> list = new ArrayList<FlowInfo>();
		String sql = "SELECT * FROM FlowInfo";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				String familyName = rs.getString("familyName");
				Timestamp birthday = rs.getTimestamp("birthday");
				int partyStanding = rs.getInt("partyStanding");
				String education = rs.getString("education");
				String posted = rs.getString("posted");
				Timestamp deliveryHours = rs.getTimestamp("deliveryHours");
				String exitType = rs.getString("exitType");
				String outgoingFlow = rs.getString("outgoingFlow");
				String partyOrganizationCategory = rs.getString("partyOrganizationCategory");
				String contactWithPartyBranch = rs.getString("contactWithPartyBranch");
				int mobilePartyCardResult = rs.getInt("mobilePartyCard");
				boolean mobilePartyCard = false;
				if (mobilePartyCardResult == 1) {
					mobilePartyCard = true;
				}
				String outflowBranchContactName = rs.getString("outflowBranchContactName");
				String outflowBranchContactPhone = rs.getString("outflowBranchContactPhone");
				String inflowBranchContactName = rs.getString("inflowBranchContactName");
				String inflowBranchContactPhone = rs.getString("inflowBranchContactPhone");
				String contactNumber = rs.getString("contactNumber");
				int qqNumber = rs.getInt("qqNumber");
				String wechatNumber = rs.getString("wechatNumber");
				int departmentId = rs.getInt("departmentId");
				list.add(new FlowInfo(id, name, sex, familyName, DateUtil.timestempToDate(birthday), partyStanding,
						education, posted, DateUtil.timestempToDate(deliveryHours), exitType, outgoingFlow,
						partyOrganizationCategory, contactWithPartyBranch, mobilePartyCard, outflowBranchContactName,
						outflowBranchContactPhone, inflowBranchContactName, inflowBranchContactPhone, contactNumber,
						qqNumber, wechatNumber, departmentId));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public FlowInfo findById(int id) {
		FlowInfo flow = null;
		String sql = "SELECT * FROM FlowInfo WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			while (rs.next()) {
				int uid = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				String familyName = rs.getString("familyName");
				Timestamp birthday = rs.getTimestamp("birthday");
				int partyStanding = rs.getInt("partyStanding");
				String education = rs.getString("education");
				String posted = rs.getString("posted");
				Timestamp deliveryHours = rs.getTimestamp("deliveryHours");
				String exitType = rs.getString("exitType");
				String outgoingFlow = rs.getString("outgoingFlow");
				String partyOrganizationCategory = rs.getString("partyOrganizationCategory");
				String contactWithPartyBranch = rs.getString("contactWithPartyBranch");
				int mobilePartyCardResult = rs.getInt("mobilePartyCard");
				boolean mobilePartyCard = false;
				if (mobilePartyCardResult == 1) {
					mobilePartyCard = true;
				}
				String outflowBranchContactName = rs.getString("outflowBranchContactName");
				String outflowBranchContactPhone = rs.getString("outflowBranchContactPhone");
				String inflowBranchContactName = rs.getString("inflowBranchContactName");
				String inflowBranchContactPhone = rs.getString("inflowBranchContactPhone");
				String contactNumber = rs.getString("contactNumber");
				int qqNumber = rs.getInt("qqNumber");
				String wechatNumber = rs.getString("wechatNumber");
				int departmentId = rs.getInt("departmentId");
				flow = new FlowInfo(uid, name, sex, familyName, DateUtil.timestempToDate(birthday), partyStanding,
						education, posted, DateUtil.timestempToDate(deliveryHours), exitType, outgoingFlow,
						partyOrganizationCategory, contactWithPartyBranch, mobilePartyCard, outflowBranchContactName,
						outflowBranchContactPhone, inflowBranchContactName, inflowBranchContactPhone, contactNumber,
						qqNumber, wechatNumber, departmentId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flow;
	}

}
